<template>
  <!-- 添加或修改查获登记结果对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="时间" prop="sj">
        <el-date-picker
          v-model="form.sj"
          clearable
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="地点" prop="dd">
        <el-input v-model="form.dd" placeholder="请输入地点" />
      </el-form-item>
      <el-form-item label="结果" prop="jg">
        <el-input v-model="form.jg" placeholder="请输入结果" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm"
        >确 定</el-button
      >
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  getChdjResult,
  addChdjResult,
  updateChdjResult,
} from "@/api/user/chdjResult";
export default {
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      buttonLoading: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sj: [{ required: true, message: "时间不能为空", trigger: "blur" }],
        dd: [{ required: true, message: "地点不能为空", trigger: "blur" }],
        jg: [{ required: true, message: "结果不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    openModal(userId, chdjResultId) {
      this.reset();
      this.open = true;
      this.title = "添加查获结果";
      this.form.userChdjId = userId;
      if (chdjResultId) {
        this.title = "编辑查获结果";
        getChdjResult(chdjResultId).then((res) => {
          this.form = res.data;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sj: undefined,
        dd: undefined,
        jg: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.chdjResultId != null) {
            updateChdjResult(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit("data-updated");
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addChdjResult({ ...this.form })
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$emit("data-updated");
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
  },
};
</script>
